package com.unit.csv;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;

import com.unit.constant.SpecialField;
import com.unit.enumeration.CsvCharset;
import com.unit.util.TimeUtil;

import au.com.bytecode.opencsv.CSVReader;

public class CSVFileReader {

	private List<String[]> allRecord;

	private String filePath;

	private Map<Long, Map<String, String>> berforeDataMap = new ConcurrentHashMap<Long, Map<String, String>>();
	private Map<Long, Map<String, String>> afterDataMap = new ConcurrentHashMap<Long, Map<String, String>>();

	public CSVFileReader(String filePath) throws FileNotFoundException, IOException {
		this.filePath = filePath;
		readCSV(CsvCharset.UTF);
	}

	public CSVFileReader(String filePath, CsvCharset charset) throws FileNotFoundException, IOException {
		this.filePath = filePath;
		readCSV(charset);
	}

	private void readCSV(CsvCharset charset) throws FileNotFoundException, IOException {
		CSVReader reader = null;
		try {
			reader = new CSVReader(
					new InputStreamReader(getClass().getResourceAsStream(filePath), charset.getCharset()));
			allRecord = reader.readAll();
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} finally {
			reader.close();
		}

		String[] tableColumn = getTableColumn();

		for (int row = 1; row < allRecord.size(); row++) {
			String[] cell = allRecord.get(row);
			String id = cell[1];
			if (!StringUtils.isNumeric(id)) {
				throw new RuntimeException("csv主键ID值不正确");
			}
			
			String csvSign = cell[0];
			Map<String, String> map = new ConcurrentHashMap<>();
			for (int i = 0; i < tableColumn.length; i++) {
				String v = cell[i+1];
				//TODO:为了处理csv日期yyyy-MM-dd HH:mm:ss 打开默认不加ss秒结构。减免数据格式化工作量，默认转加:00补全。
				if(TimeUtil.isTimeString(v)) {
					if(v.split(":").length == 2) {
						v+= ":00";
					}
				}
				map.put(tableColumn[i], v);				
			}		
						
			switch (csvSign) {
			case SpecialField.csvDataBefore:
				berforeDataMap.put(Long.valueOf(id), map);
				break;
			case SpecialField.csvDataAfter:
				afterDataMap.put(Long.valueOf(id), map);
				break;
			default:
				throw new RuntimeException("csv第一列标识字段值不正确，请@see SpecialField");
			}

		}
	}


	public Map<Long, Map<String, String>> getAfterDataMap() {
		return afterDataMap;
	}

	public Map<Long, Map<String, String>> getBerforeDataMap() {
		return berforeDataMap;

	}

	public String[] getTableColumn() {
		String[] csvTitle = allRecord.get(0);
		String[] tableColumn = new String[csvTitle.length - 1];
		System.arraycopy(csvTitle, 1, tableColumn, 0, csvTitle.length - 1);
		return tableColumn;
	}

	public String getTableName() {
		URL url = this.getClass().getResource(filePath);
		String fileName = new File(url.getFile()).getName();
		String tableName = StringUtils.substringBeforeLast(fileName, ".").trim();
		return tableName;
	}

	public List<String[]> getAllRecord() {
		return allRecord;
	}

}